//
//  SEGLayoutMacro.m
//  FrameworkTest
//
//  Created by 山刘 on 2018/7/21.
//  Copyright © 2018年 刘山. All rights reserved.
//

#import "SEGLayoutMacro.h"
@implementation SEGLayoutMacro
+ (NSString*)segImageWithName:(NSString*)name {
    if (!name || name.length <= 0) return @"";
    NSString *path = @"";
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"Commom" withExtension:@"bundle"];
    if (url) {
        NSMutableString *urlPath = [NSMutableString stringWithString:[url absoluteString]];
        NSString *fileName =  ([name rangeOfString:@"#"].location == NSNotFound) ? @"": [[name componentsSeparatedByString:@"#"] firstObject];
        if ([fileName length] > 0) [urlPath appendFormat:@"%@/",fileName];
        path = [[[NSBundle bundleWithURL:[NSURL URLWithString:urlPath]] resourcePath] stringByAppendingPathComponent:name];
    }
    return path;
}

+(UIImage *)segAutoImageWithName:(NSString*)name filePath:(NSString *)file {
    NSString *commonImagePath = [self segImageWithName:name];
    UIImage *commonImage = [UIImage imageWithContentsOfFile:commonImagePath];
    if (commonImage.size.width != 0) {
        return commonImage;
    }
    NSArray *pathArr = [file componentsSeparatedByString:@"/"];
    NSMutableArray *arrM = [NSMutableArray array];
    NSString *modelName;
    //查找模块名称
    for (NSString *n in pathArr) {
        if (![arrM containsObject:n]) {
            [arrM addObject:n];
        }else {
            modelName = n;
            break;
        }
    }
    //防止common bundle 重名
    if (![modelName containsString:@"SEG"]) {
        modelName = [NSString stringWithFormat:@"SEG%@",modelName];
    }
    NSString *fileName = [pathArr.lastObject componentsSeparatedByString:@"."].firstObject;
    NSBundle *currentBundle = [NSBundle bundleForClass:NSClassFromString(fileName)];
    NSURL *url = [currentBundle URLForResource:modelName withExtension:@"bundle"];
    if (url) {
        NSMutableString *urlPath = [NSMutableString stringWithString:[url absoluteString]];
        NSString *fileName =  ([name rangeOfString:@"#"].location == NSNotFound) ? @"": [[name componentsSeparatedByString:@"#"] firstObject];
        if ([fileName length] > 0) [urlPath appendFormat:@"%@/",fileName];
        NSString *path = [[[NSBundle bundleWithURL:[NSURL URLWithString:urlPath]] resourcePath] stringByAppendingPathComponent:name];
        return [UIImage imageWithContentsOfFile:path];
    }
    return nil;
}

@end

@implementation UIView (Base)
-(void)setLy_x:(CGFloat)ly_x{
    CGRect frame = self.frame;
    frame.origin.x = LYDistanceV(ly_x);
    self.frame = frame;
}

- (CGFloat)ly_x
{
    return self.frame.origin.x;
}

-(void)setLy_y:(CGFloat)ly_y{
    CGRect frame = self.frame;
    frame.origin.x = LYDistanceH(ly_y);
    self.frame = frame;
}

- (CGFloat)ly_y
{
    return self.frame.origin.y;
}

-(void)setLy_left:(CGFloat)ly_left{
    CGRect frame = self.frame;
    frame.origin.x = LYDistanceV(ly_left);
    self.frame = frame;
}

-(CGFloat)ly_left{
    return self.frame.origin.x;
}

-(void)setLy_top:(CGFloat)ly_top{
    CGRect frame = self.frame;
    frame.origin.y = LYDistanceH(ly_top);
    self.frame = frame;
}

-(CGFloat)ly_top{
    return self.frame.origin.y;
}

-(void)setLy_right:(CGFloat)ly_right{
    CGRect frame = self.frame;
    frame.origin.x = LYDistanceV(ly_right) - frame.size.width;
    self.frame = frame;
}

-(CGFloat)ly_right{
    return self.frame.origin.x + self.frame.size.width;
}

-(void)setLy_bottom:(CGFloat)ly_bottom{
    CGRect frame = self.frame;
    frame.origin.y = LYDistanceH(ly_bottom) - frame.size.height;
    self.frame = frame;
}

-(CGFloat)ly_bottom{
    return self.frame.origin.y + self.frame.size.height;
}

-(void)setLy_width:(CGFloat)ly_width{
    CGRect frame = self.frame;
    frame.size.width = LYDistanceV(ly_width);
    self.frame = frame;
}

-(CGFloat)ly_width{
    return self.frame.size.width;
}

-(void)setLy_height:(CGFloat)ly_height{
    CGRect frame = self.frame;
    frame.size.height = LYDistanceH(ly_height);
    self.frame = frame;
}

-(CGFloat)ly_height{
    return self.frame.size.height;
}

-(void)setLy_centerX:(CGFloat)ly_centerX{
    self.center = CGPointMake(LYDistanceV(ly_centerX), self.center.y);
}

-(CGFloat)ly_centerX{
    return self.center.x;
}

-(void)setLy_centerY:(CGFloat)ly_centerY{
    self.center = CGPointMake(self.center.x, LYDistanceH(ly_centerY));
}

-(CGFloat)ly_centerY{
    return self.center.y;
}

-(void)setLy_origin:(CGPoint)ly_origin{
    CGRect frame = self.frame;
    frame.origin = LYPointMake(ly_origin.x, ly_origin.y);
    self.frame = frame;
}

-(CGPoint)ly_origin{
    return self.frame.origin;
}

-(void)setLy_size:(CGSize)ly_size{
    CGRect frame = self.frame;
    frame.size = LYSizeMake(ly_size.width, ly_size.height);
    self.frame = frame;
}

-(CGSize)ly_size{
    return self.frame.size;
}

//- (void)setX:(CGFloat)x
//{
//    CGRect frame = self.frame;
//    frame.origin.x = x;
//    self.frame = frame;
//}
//
//- (void)setY:(CGFloat)y
//{
//    CGRect frame = self.frame;
//    frame.origin.y = y;
//    self.frame = frame;
//}
//
//- (CGFloat)x
//{
//    return self.frame.origin.x;
//}
//
//- (CGFloat)y
//{
//    return self.frame.origin.y;
//}
//
//- (CGFloat)left {
//    return self.frame.origin.x;
//}
//
//- (void)setLeft:(CGFloat)x {
//    CGRect frame = self.frame;
//    frame.origin.x = x;
//    self.frame = frame;
//}
//
//- (CGFloat)top {
//    return self.frame.origin.y;
//}
//
//- (void)setTop:(CGFloat)y {
//    CGRect frame = self.frame;
//    frame.origin.y = y;
//    self.frame = frame;
//}
//
//- (CGFloat)right {
//    return self.frame.origin.x + self.frame.size.width;
//}
//
//- (void)setRight:(CGFloat)right {
//    CGRect frame = self.frame;
//    frame.origin.x = right - frame.size.width;
//    self.frame = frame;
//}
//
//- (CGFloat)bottom {
//    return self.frame.origin.y + self.frame.size.height;
//}
//
//- (void)setBottom:(CGFloat)bottom {
//    CGRect frame = self.frame;
//    frame.origin.y = bottom - frame.size.height;
//    self.frame = frame;
//}
//
//- (CGFloat)width {
//    return self.frame.size.width;
//}
//
//- (void)setWidth:(CGFloat)width {
//    CGRect frame = self.frame;
//    frame.size.width = width;
//    self.frame = frame;
//}
//
//- (CGFloat)height {
//    return self.frame.size.height;
//}
//
//- (void)setHeight:(CGFloat)height {
//    CGRect frame = self.frame;
//    frame.size.height = height;
//    self.frame = frame;
//}
//
//- (CGFloat)centerX {
//    return self.center.x;
//}
//
//- (void)setCenterX:(CGFloat)centerX {
//    self.center = CGPointMake(centerX, self.center.y);
//}
//
//- (CGFloat)centerY {
//    return self.center.y;
//}
//
//- (void)setCenterY:(CGFloat)centerY {
//    self.center = CGPointMake(self.center.x, centerY);
//}
//
//- (CGPoint)origin {
//    return self.frame.origin;
//}
//
//- (void)setOrigin:(CGPoint)origin {
//    CGRect frame = self.frame;
//    frame.origin = origin;
//    self.frame = frame;
//}
//
//- (CGSize)size {
//    return self.frame.size;
//}
//
//- (void)setSize:(CGSize)size {
//    CGRect frame = self.frame;
//    frame.size = size;
//    self.frame = frame;
//}
@end
